<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Share extends Model
{
    protected $dateFormat = 'U';
    //
    protected $table = "lexiang_share";
    protected $primaryKey = 'share_id';

    /**
     * 保存分享记录
     */
    public function add($data)
    {
    	$obj = new Static;
    	$obj->share_billno = createNonceStr(28);
    	$obj->share_to_userid = $data['share_to_userid'];
    	$obj->user_id = $data['user_id'];
    	$obj->status = 0;
        $obj->toorme = $data['toorme'];
    	$obj->buy_card_id = $data['buy_card_id'];
    	$obj->card_id = $data['card_id'] ?? 0;

    	if($obj->save()){
    		return [
    			'success'=>true,
    			'code'=>'000',
    			'message'=>'保存成功'
    		];
    	}else{
    		return [
    			'success'=>false,
    			'code'=>'006',
    			'message'=>'保存出错，请稍后再试！',
    		];
    	}
    }

    public function getShareUserNameAttribute($v)
    {
        return User::where('user_id',$this->user_id)->value('nicename') ?? '空的昵称';
    }

    public function getUserNameAttribute($v)
    {
        return User::where('user_id',$this->share_to_userid)->value('nicename') ?? '空的昵称';
    }

    public function getCardNameAttribute($v)
    {
        return MealCard::where('card_id',$this->card_id)->value('card_name');
    }

    public function getGetIfAttribute($v)
    {
        $statusDe = ['0'=>'待领取','1'=>"已领取",'2'=>'已领取'];
        return $statusDe[$this->status];
    }

    public function getCreatedAtAttribute($v)
    {
        return date('Y-m-d H:i:s',$v);
    }

    public function getUpdatedAtAttribute($v)
    {
        return date('Y-m-d H:i:s',$v);
    }
}
